<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;

class Con2 extends Con4
{
    public function index()
    {
        $sql = "SELECT * FROM `lamp_role`";

        $list = Db::query($sql);
        return view('admin@con2/tables',['list' => $list]);
    }

    public function func1()
    {
        return view('admin@con2/func1', ['title' => '新增用户']);
    }

    public function func2(Request $request)
    {
        $p = $request->get();
        // var_dump($p);
        $data = [
            'name' => $p['name'],
            'status' => $p['status'],
            'remark' => $p['remark'],
       ];


        $result = Db::table('lamp_role')->data($data)->insert();

        if ($result >0 ){
            return $this->success('新增成功',url('admin/Con2/index'));
        }else{
            return $this->success('新增失败');
        }
    }

     public function addnode($id)
    {
        $sql = "SELECT * FROM `lamp_node`";
        $node = Db::query($sql);
        // var_dump($node);
        $data = Db::table('lamp_role_node')->field('nid')->where('rid = '.$id)->select();
        // var_dump($data);

        foreach ($node as $k => $v) {
            // $node[$k]['nid'] = $id;
            // var_dump($node);


            // var_dump($node[$k]['nid']);
            $node[$k]['check'] = null;
            foreach ($data as $y) {
                if ($v['id'] == $y['nid']) {
                    $node[$k]['check'] = 'checked';
                }
            }
        }
        // var_dump($node);die;
        $rid[0]= ['rid'=>$id];
        return view('admin@con2/func2',[
            'node' => $node,
            'rid' => $rid
        ]);
    }

    public function doaddnode(Request $request)
    {
        $p = $request->post();
        // var_dump($p);die;
        $rid = $p['rid'];

        Db::table('lamp_role_node')->where('rid = '.$rid)->delete();

        $a = ['rid' => $p['rid']];

        if ($p !== $a) {
            foreach ($p['nid'] as $v) {
                $data = [
                    'rid' => $p['rid'],
                    'nid' => $v
                ];
                $result = Db::table('lamp_role_node')->insert($data);
            }
        }else{
            return $this->success('新增成功',url('admin/Con2/index'));
        }


        if ($result >0 ){
            return $this->success('新增成功',url('admin/Con2/index'));
        }else{
            return $this->success('新增失败');
        }
    }
}
